package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "新增菜品相关接口")
public class DishController {
    @Autowired
    DishService dishService;

    @Autowired
    RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result savedish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品信息：{}",dishDTO);
       dishService.savedishandflavor(dishDTO);
       //获取需要清理缓存的数据
       String key= "dish_"+dishDTO.getCategoryId();
       //清理缓存数据
        cleanredis(key);
        return Result.success();

    }
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询：{}",dishPageQueryDTO);
        PageResult pageResult=dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据菜品id删除菜品
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deletedish(@RequestParam List<Long>ids){
        log.info("删除菜品:{}",ids);
        //根据菜品id删除菜品信息
        dishService.deletedish(ids);
        //清理缓存数据
        cleanredis("dish_*");
        return Result.success();

    }
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result getByIds(@PathVariable Long id){
        log.info("根据id查询菜品：{}",id);
        //获取菜品id方法
        DishVO dishVO=dishService.getDishByIds(id);
        return Result.success(dishVO);
    }

    @PutMapping
    public Result update (@RequestBody DishDTO dishDTO){
        log.info("根据前面所查询的id修改菜品信息:{}",dishDTO);
        dishService.updateandFlavor(dishDTO);
        //清除缓冲数据
        cleanredis("dish_*");
        return Result.success();
    }
    @GetMapping("/list")
    public Result <List> list(Integer categoryId,String name){
        log.info("根据id查询菜品遍历出来:{}",categoryId,name);
        List<Dish> list=dishService.list(categoryId,name);
        return Result.success(list);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用菜品状态")
    public Result startstop(@PathVariable("status") Integer status,Long id){
        dishService.startstop(status,id);
        //清除缓冲数据
        cleanredis("dish_*");
        return Result.success();
    }



    /**
     * 清理缓存数据
     * @param redis
     */
    private void cleanredis(String redis){
        //获取缓冲数据的key值    定义清理缓存数据的方法
        Set keys = redisTemplate.keys(redis);
        redisTemplate.delete(keys);
    }

    }




